package com.hsck.order.kds.setup.fragment.dyjgl

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentPrinterBrandBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.adapter.PrinterBrandAdapter
import kotlinx.android.synthetic.main.common_head_black.*

/**
 * 打印机品牌
 */
class PrinterBrandFragment : BaseFragment<FragmentPrinterBrandBinding>(),
    SetupActivity.UpdatableFragment {


    var type = ""
    var topFragment: Fragment? = null
    private val adapter by lazy { PrinterBrandAdapter(R.layout.item_printer_brand) }


    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentPrinterBrandBinding {
        return FragmentPrinterBrandBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        tv_head_black_title.text = "打印机品牌"
        binding.apply {
            rvPrinterBrand.layoutManager = GridLayoutManager(requireActivity(), 2)
            rvPrinterBrand.adapter = adapter
            adapter.setSingleChoice(rvPrinterBrand, R.id.cb_item_printer_brand, true)
            val list = arrayListOf<CategoryBean>()
            for (i in 1..6) {
                list.add(CategoryBean("新北洋$i", false))
            }
            adapter.datas = list
            //确认
            btnPrinterBrandQr.setOnClickListener {
                val list = adapter.getSelect() as ArrayList<CategoryBean>
                if (list.size > 0) {
                    //给上级页面传值
                    LiveDataBus.with<ArrayList<CategoryBean>>("PrinterBrandFragment").postData(list)
                    setBack()
                } else {
                    Toaster.show("请选择打印机品牌")
                }

            }

        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

}